package com.sasis.webapp.action;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessages;

import com.sasis.Constants;
import com.sasis.model.UserInfo;
import com.sasis.service.DeptInfoService;
import com.sasis.service.UserInfoService;
import com.sasis.util.StringUtil;
import com.sasis.webapp.form.UserInfoBean;
import com.sasis.webapp.util.ListBox;

/**
 * 
 * @create by kellen
 * @create on 2006-12-22
 */
public class UserInfoAction  extends BaseAction {
	 public ActionForward execute(ActionMapping mapping, ActionForm form,
             HttpServletRequest request,
             HttpServletResponse response)
	 throws Exception {
		 log.debug(">>>>>>>>login in here>>>");
		 String action = request.getParameter("action");
		
		 String algorithm = (String) getConfiguration().get(Constants.ENC_ALGORITHM);
		 
		 ActionMessages errors =  form.validate(mapping, request);
		 UserInfoBean userInfoBean = (UserInfoBean)form;
		
		 UserInfoService userInfoService = (UserInfoService) getBean("userInfoService");
		 DeptInfoService deptInfoService = (DeptInfoService) getBean("deptInfoService");
		  
		 log.debug(">>>>>>>>>userInfoService>>>>>>>"+userInfoService);
		 String forward = "success";
		 
		 if(StringUtil.isEmpty(action)){
			 
			 forward = userInfoBean.userForward(request,userInfoService,deptInfoService);
			 
		 }else if("view".equalsIgnoreCase(action)){
			 
			 forward = userInfoBean.getUserInfo(request,userInfoService,deptInfoService);
			 
		 }
		 else if("userInfo".equalsIgnoreCase(action)){
			 
			 forward = userInfoBean.getUserInfo(request,userInfoService,deptInfoService);
			 
		 }else if("userList" .equalsIgnoreCase(action)){
			 
			 forward = userInfoBean.getAllUser(request,userInfoService);
			 if(null != request.getParameter("toChoose"))
				 forward = "toChoose";	 			 
		 }
		 else if("preInsert" .equalsIgnoreCase(action)){
			 
			 forward = userInfoBean.userPreInsert(request,deptInfoService);
			 
		 }else if("insert" .equalsIgnoreCase(action)){ 
			 if (!errors.isEmpty()) {
		    	 saveErrors(request, errors); 
		     }else forward = userInfoBean.insert(request,userInfoService,errors,algorithm);
		     
		 }
		 else if("update" .equalsIgnoreCase(action)){
			 //save errors 
			 if(!errors.isEmpty()){
				 saveErrors(request, errors);
				 return mapping.findForward("success");
			 }
			 forward = userInfoBean.update(request,userInfoService,null);
			 
		 }else if("delete" .equalsIgnoreCase(action)){ 
			 forward = userInfoBean.delete(request,userInfoService,errors);
			 //save errors 
			 if(!errors.isEmpty()){
				 saveErrors(request, errors);
			 }
			 
		 }else if("logout" .equalsIgnoreCase(action)){   
			 forward = userInfoBean.logout(request,userInfoService,errors);
			 //save errors 
			 if(!errors.isEmpty()){
				 saveErrors(request, errors);
			 }
			 
		 }else if("import" .equalsIgnoreCase(action)){   
			 forward = userInfoBean.upload(request,userInfoService,deptInfoService,errors);
			 //save errors 
			 if(!errors.isEmpty()){
				 saveErrors(request, errors);
			 }
			 
		 }
		 else if("userRoleSet" .equalsIgnoreCase(action)){
			 ArrayList list = (ArrayList)userInfoService.getRoleAll();
			 log.debug("user action list>>"+list);
			 request.setAttribute("roleList", ListBox.roleListBox(list));
			 //userInfoBean.setRoleList(ListBox.roleListBox(list));
			 
			ArrayList rtlist = (ArrayList)userInfoService.getUserRoleRefByPK(userInfoBean.getSeledId());
				//userInfoBean.setRtRoleList(ListBox.roleListBox(rtlist));
			request.setAttribute("rtRoleList", ListBox.roleListBox(rtlist));
		 }else if("userRightSet" .equalsIgnoreCase(action)){
			 ArrayList list = (ArrayList)userInfoService.getRightAll();
			 log.debug("user action list>>"+list); 
			 userInfoBean.setRightList(ListBox.roleListBox(list));
			 
			 ArrayList rtlist = (ArrayList)userInfoService.getUserRightRefByPK(userInfoBean.getSeledId());
			userInfoBean.setRtRightList(ListBox.roleListBox(rtlist)); 
		 }else if("userRightAdd" .equalsIgnoreCase(action)){
			 
				log.debug(">>>>>>>>>>>id>>>"+userInfoBean.getAryId());
				String[] strs = userInfoBean.getAryId().split(",");
				//delete user right by userinfo
				UserInfo record = new UserInfo();
				record.setSeledId(userInfoBean.getSeledId());
				userInfoService.deleteUserRightRefByPK(record);
				if(null!=strs && !"".equals(strs[0])){
					for(int i=0;i<strs.length;i++){
						record = new UserInfo();
						BeanUtils.copyProperties(record, userInfoBean);
						record.setFuncId(strs[i]);
						log.debug("right id>>"+record.getFuncId());
						userInfoService.updateUserRightRefByPK(record);  
					}
				}
				ArrayList list = (ArrayList)userInfoService.getUserRightRefByPK(userInfoBean.getSeledId());
				userInfoBean.setRtRightList(ListBox.roleListBox(list)); 
			  forward = "success";
		 }else if("userRoleAdd" .equalsIgnoreCase(action)){ 
			
			 log.debug(">>>>>>>>>>>id>>>"+userInfoBean.getAryId());
			 String[] strs = userInfoBean.getAryId().split(",");
				//delete user right by userinfo
				UserInfo record = new UserInfo();
				record.setSeledId(userInfoBean.getSeledId());
				userInfoService.deleteUserRoleRefByPK(record);
				log.debug(">>>>>>>>flag>>>>>--strs[0]"+strs[0]+">selected id>"+userInfoBean.getSeledId());
				if(null!=strs&&!"".equals(strs[0])){
					for(int i=0;i<strs.length;i++){ 
						record = new UserInfo();
						BeanUtils.copyProperties(record, userInfoBean);
						record.setRoleId(strs[i]);
						log.debug("role id>>"+record.getRoleId());
						userInfoService.updateUserRoleRefByPK(record);
					}
				}
				
				ArrayList rolelist = (ArrayList)userInfoService.getRoleAll();
			 
				 request.setAttribute("roleList", ListBox.roleListBox(rolelist));
				 
				ArrayList list = (ArrayList)userInfoService.getUserRoleRefByPK(userInfoBean.getSeledId());
				request.setAttribute("rtRoleList", ListBox.roleListBox(list));
				request.setAttribute("message","success");
				//userInfoBean.setRtRoleList(ListBox.roleListBox(list)); 
			 forward = "success";
		 }
		 
		

		 log.debug(">>>>>>>forward>>>>"+forward); 

	        return mapping.findForward(forward);
		 
	 }
}
